/*
 * WeakSoftSyncHashMap.java
 *
 * Created on January 13, 2006, 9:00 AM
 *
 * To change this basis, choose Tools | Options and locate the basis under
 * the Source Creation and Management node. Right-click the basis and choose
 * Open. You can then make changes to the basis in the Source Editor.
 */

package com.xucia.jsponic.util;

import java.lang.ref.SoftReference;
import java.util.Hashtable;

/**
 * 
 * Strong references to keys, and soft references to values, used for caching
 */
public class StrongSoftSyncHashMap<K, V> extends Hashtable {
	public StrongSoftSyncHashMap(int size) {
		super(size);
	}

	@Override
	public synchronized V get(Object key) {
		SoftReference<V> ref = (SoftReference<V>) super.get(key);
		if (ref == null)
			return null;
		V val = ref.get();
		if (val == null)
			super.remove(key);
		return val;
	}

	@Override
	public synchronized V put(Object key, Object value) {
		return (V) super.put(key, new SoftReference(value));
	}

	@Override
	public synchronized V remove(Object key) {
		SoftReference<V> ref = (SoftReference<V>) super.remove(key);
		if (ref == null)
			return null;
		return ref.get();
	}
}
